import com.sun.org.apache.xpath.internal.objects.XNodeSet;

import java.util.HashMap;

public class MySingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        this.head = listNode1;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            size++;
        }
        return size;
    }

    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (key == cur.val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }

    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        ListNode cur = head;
        if (head == null) {
            head = listNode;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = listNode;
        }
    }

    public void addIndex(int index, int data) throws IndexException {

        ListNode listNode = new ListNode(data);
        //中间部分
        if (index < 0 || index > size()) {
            throw new IndexException("index不合法" + index);
        }
//        if(head == null) {
//            head = listNode;
//            return;
//        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(size());
            return;
        }
        ListNode cur = searchPrevIndex(index);
        listNode.next = cur.next;
        cur.next = listNode;

    }

    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public void remove(int key) {

        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findPrevKey(key);
        if (cur == null) {
            return;//没有要删除的数字
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            } else {
                cur = cur.next;
            }
        }
        return null;
    }

    public ListNode removeAllKey(int key) {
        if (head == null) {
            return null;
        }
        ListNode perv = head;
        ListNode cur = perv.next;
        while (cur != null) {
            if (cur.val == key) {
                perv.next = cur.next;
                cur = cur.next;
            } else {
                perv = cur;
                cur = cur.next;
            }
        }
        //除了头节点，都删除完成了
        if (key == head.val) {//不能用cur->空指针异常
            head = head.next;
        }
        return head;
    }

    public void clear() {
        head = null;
    }

    //下面是练习
    //1.反转链表
    //12 23 45 56->56 45 23 12
    public ListNode ReverseTheList() {

        if (head == null || head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //2.给定一个带有头结点 head 的非空单链表，返回链表的中间结点。如果有两个中间结点，则返回第二个中间结点
    //法1：快慢指针
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {//fast和fast.next位置不能换
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //法2：
    public ListNode middleNode1() {
        ListNode cur = head;
        int mid = 0;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        ListNode prev = head;
        ListNode p = head.next;
        while (mid != count / 2) {
            p = p.next;
            mid++;
            prev = prev.next;
        }
        return prev;
    }

    //3.输入一个链表，输出该链表中倒数第k个结点
    //法1：
    public ListNode findIndexLastVal(int k) throws IndexException {

        ListNode cur = head;
        ListNode p = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        if (k <= 0 || k > count) {
            throw new IndexException("k不合法" + k);
        }

        int index = count - k;
        while (index > 0) {
            p = p.next;
            index--;
        }
        return p;
    }

    //法2：
    public ListNode findIndexLastVal1(int k) throws IndexException {
        ListNode slow = head;
        ListNode fast = head;

        if(head == null) {
            return null;
        }
        int count = 0;
        while (count != k-1) {
            if(fast.next != null) {
                fast = fast.next;
                count++;
            }else {
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //4.将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    public static MySingleList.ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        MySingleList.ListNode newHead = new MySingleList.ListNode(-1);
        MySingleList.ListNode tmp = newHead;
        while(headA != null && headB != null) {
            if(headA.val < headB.val) {
                tmp.next = headA;
                headA = headA.next;
                tmp = tmp.next;
            }else {
                tmp.next = headB;
                headB = headB.next;
                tmp = tmp.next;
            }
        }
        if( headA != null) {
             tmp.next = headA;
        }
        if( headB != null) {
            tmp.next = headB;
        }
        return newHead.next;
    }
    //5.以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前
    public ListNode Partition(int val) {
        ListNode cur = head;
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        if(cur == null) {
            return null;
        }

        while(cur != null) {
            if (cur.val >= val) {
                if (as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            } else {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }
            cur = cur.next;
        }
            if(bs == null) {
                return as;
            }
            be.next = as;
            if(as != null) {
                ae.next = null;
            }

        return bs;
    }
  //6.判断回文链表
    public boolean palindromeList() {
//        if(head == null) {
//            return false;
//        }
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        slow.next = null;

        //ListNode curNext = cur.next;不能在这里
        //翻转链表
        while(cur != null) {
            ListNode curNext = cur.next;//要在这里
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
//        while(slow != head) {
        while(slow != null) {
            if(slow.val != head.val) {//1
                return false;
            }
//            if (head.next == slow) {
//                return true;
//            }
            slow = slow.next;
            head = head.next;

        }
        return true;
    }
    //7.输入两个链表，找出它们的第一个公共结点
    public ListNode getIntersectionNode(ListNode headA,ListNode headB) {
        ListNode A = headA;
        ListNode B = headB;
        int count_A = 0;
        int count_B = 0;
        while(A != null) {
            A = A.next;
            count_A++;
        }
        while (B != null) {
            B = B.next;
            count_B++;
        }
        int k = count_A - count_B;

        while (k != 0) {
            if(k > 0) {
                headA = headA.next;
                k--;
            } else{
                headB = headB.next;
                k++;
            }
        }
        while(headA != null ) {
            if (headA != headB) {
                headA = headA.next;
                headB = headB.next;
            }else{
                return headA;
            }
        }
        return null;//headA、headB为空时直接返回null了
    }
    //创建交叉链表
    public ListNode createYList1(MySingleList.ListNode headA,MySingleList.ListNode headB) {
        headB.next = headA.next.next;
        return headA;
    }
    public  ListNode createYList() {
        // 创建链表 A: 1 -> 3 -> 5 -> 7
        ListNode a1 = new ListNode(1);
        ListNode a2 = new ListNode(3);
        ListNode a3 = new ListNode(5);
        ListNode a4 = new ListNode(7);
        a1.next = a2;
        a2.next = a3;
        a3.next = a4;

        // 创建链表 B: 2 -> 4 -> 6
        ListNode b1 = new ListNode(2);
        ListNode b2 = new ListNode(4);
        ListNode b3 = new ListNode(6);
        ListNode b4 = new ListNode(7);
        b1.next = b2;
        b2.next = b3;

        // 将链表 B 的末尾节点指向链表 A 的第二个节点，形成交叉
        b3.next = a4;

        // 返回链表 A 的头节点
        ListNode headA = a1;
        return  headA;
    }
    //8.判断链表是否有环
    public boolean hascycle() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    //9.找出环形链表的交点
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null ||fast.next == null) {
            return null;
        }
        slow = head;
        while(slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
//    public ListNode cycleList(MySingleList.ListNode head) {
//        ListNode cur = head;
//        while(cur.next != null) {
//            cur = cur.next;
//        }
//
//        cur.next = head.next.next;
//        return head;
//    }
public ListNode cycleList(MySingleList.ListNode head) {
    if (head == null || head.next == null) {
        return null; // 如果链表为空或者只有一个节点，无法形成环，直接返回null
    }

    ListNode slow = head;
    ListNode fast = head;

    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;

        if (slow == fast) { // 如果快慢指针相遇，说明链表中有环
            ListNode start = head; // 从头节点开始移动，直到和慢指针相遇，相遇的地方即为环的起点
            while (start != slow) {
                start = start.next;
                slow = slow.next;
            }
            return start; // 返回环的起点
        }
    }

    return null; // 链表中无环
}
}